package com.xz.knowledgeDuplication.pattern.memento;

/**
 * 游戏角色(发起人角色)
 *
 * @author 召
 * @date 2022-03-31 21:07:41
 */
public class GameRole {

    /**
     * 生命力
     */
    private int vit;

    /**
     * 攻击力
     */
    private int atk;

    /**
     * 防御力
     */
    private int def;

    /**
     * 初始化状态
     */
    public void initState() {
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    /**
     * 战斗
     */
    public void fight() {
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    /**
     * 创建存档
     *
     * @return 存档
     */
    public Memento saveState() {
        return new RoleStateMemento(this.vit, this.atk, this.def);
    }

    /**
     * 读档
     *
     * @param memento 存档
     */
    public void recoverState(Memento memento) {
        RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
        this.vit = roleStateMemento.getVit();
        this.atk = roleStateMemento.getAtk();
        this.def = roleStateMemento.getDef();
    }

    @Override
    public String toString() {
        return "GameRole{" +
                "vit=" + vit +
                ", atk=" + atk +
                ", def=" + def +
                '}';
    }

    /**
     * 角色状态备忘录(备忘录角色)
     */
    private class RoleStateMemento implements Memento{
        /**
         * 生命力
         */
        private int vit;

        /**
         * 攻击力
         */
        private int atk;

        /**
         * 防御力
         */
        private int def;

        public RoleStateMemento() {
        }

        public RoleStateMemento(int vit, int atk, int def) {
            this.vit = vit;
            this.atk = atk;
            this.def = def;
        }

        public int getVit() {
            return vit;
        }

        public void setVit(int vit) {
            this.vit = vit;
        }

        public int getAtk() {
            return atk;
        }

        public void setAtk(int atk) {
            this.atk = atk;
        }

        public int getDef() {
            return def;
        }

        public void setDef(int def) {
            this.def = def;
        }
    }
}
